package edu.jit.nsi.iot_ms.transport.httpclient;

import edu.jit.nsi.iot_ms.transport.httpclient.jsiot.JSPlatData;
import edu.jit.nsi.iot_ms.transport.httpclient.jsiot.JSPlatRes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;

@Slf4j
@Service
public class HttpClientGate {

    private RestTemplate restTemplate = getInstance("utf-8");

    public static RestTemplate getInstance(String charset) {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new StringHttpMessageConverter(Charset.forName(charset)));
        return restTemplate;
    }


    public boolean postMsg2JSPlat(JSPlatData jsRptMsg) {
        String url = "http://210.12.220.220:8012/dataswitch-sq/sendApi/sendSingleInfo";

//        JSPlatRes rpttRes = restTemplate.postForObject(url, jsRptMsg, JSPlatRes.class);
//        if(rpttRes.getCode()==200)
//            return true;
//        else
//            return false;
        String rpttRes = restTemplate.postForObject(url, jsRptMsg, String.class);
        if(rpttRes.contains("200"))
            return true;
        else
            return false;
    }


}
